package com.ctgu.tengyun.bean.convert;

import com.ctgu.tengyun.bean.request.SaveProductInfoRequest;
import com.ctgu.tengyun.bean.response.ProductDetail;
import com.ctgu.tengyun.bean.response.ProductListInfoResponse;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.ProductStatus;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/7/21.
 */
public class ProductInfoConvert {

    public static ProductListInfoResponse toProductListInfoResponse(Integer totalCount,
                                                                    Integer pageSize,
                                                                    List<ProductInfo> productInfoList) {
        ProductListInfoResponse response = new ProductListInfoResponse();
        response.setTotalCount(totalCount);
        response.setTotalPage((int) Math.ceil(totalCount / (double) pageSize));

        List<ProductDetail> productDetailList = new ArrayList<>(productInfoList.size());
        for (ProductInfo productInfo : productInfoList) {
            productDetailList.add(toProductDetail(productInfo));
        }

        response.setProductList(productDetailList);
        return response;
    }

    public static ProductDetail toProductDetail(ProductInfo productInfo) {
        if (null == productInfo) {
            return null;
        }
        ProductDetail productDetail = new ProductDetail();
        productDetail.setId(productInfo.getId());
        productDetail.setName(productInfo.getName());
        productDetail.setProductNo(productInfo.getProductNo());
        productDetail.setVideoFile(FileConvert.fillFileBeanUrlPath(productInfo.getVideoFile()));
        productDetail.setDesignFile(FileConvert.fillFileBeanUrlPath(productInfo.getDesignFile()));
        productDetail.setElectricFile(FileConvert.fillFileBeanUrlPath(productInfo.getElectricFile()));
        productDetail.setProductStatus(productInfo.getProductStatus());
        productDetail.setFactoryCode(productInfo.getFactoryCode());
        if (null != productInfo.getProduceDate()) {
            productDetail.setProduceDate(productInfo.getProduceDate().getTime());
        }
        productDetail.setCreateTime(productInfo.getCreateTime());

        return productDetail;
    }

    public static ProductInfo fromSaveProductInfoRequest(SaveProductInfoRequest request) {
        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "productNo is required.");
        Validator.assertHasFields(!StringUtils.isEmpty(request.getName()), "productName is required.");
        FileConvert.splitFileBeanUrlPath(request.getVideoFile());
        FileConvert.splitFileBeanUrlPath(request.getDesignFile());
        FileConvert.splitFileBeanUrlPath(request.getElectricFile());

        ProductInfo productInfo = new ProductInfo();
        if (null != request.getId()) {
            productInfo.setId(request.getId());
        }

        productInfo.setName(request.getName());
        productInfo.setProductNo(request.getProductNo());
        productInfo.setProductStatus(null != request.getProductStatus() ? request.getProductStatus() : ProductStatus.normal.getValue());

        if (StringUtils.isNotEmpty(request.getFactoryCode())) {
            productInfo.setFactoryCode(request.getFactoryCode());
        }

        if (null != request.getProduceDate()) {
            productInfo.setProduceDate(new Date(request.getProduceDate()));
        }

        productInfo.setVideoFile(request.getVideoFile());
        productInfo.setDesignFile(request.getDesignFile());
        productInfo.setElectricFile(request.getElectricFile());

        return productInfo;
    }

}
